package edu.spec.chenli.Listener;

import edu.spec.chenli.Servlet.DbDao;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

//@WebListener()
public class Online_Listener implements ServletContextListener{
    //超过十分钟没有访问本站即认为用户已经离线
    public final  int  MAX_MILLIS=10*60*1000;
    //应用启动时触发该方法
    public void contextInitialized(ServletContextEvent sce) {
   //每五秒检查一次
        new javax.swing.Timer(1000 * 5, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                DbDao dd=new DbDao("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/1613580007_1",
                        "root","123456789");
                ResultSet rs= dd.query("select * from online_inf",false);
                    StringBuffer beRemove=new StringBuffer("(");
                    while (rs.next()) {
                        //如果距离上次访问时间超过了指定的时间

                        if ((System.currentTimeMillis() - rs.getLong(5) )> MAX_MILLIS) {
                            //将需要被删除的session ID 添加进来
                            beRemove.append("'");
                            beRemove.append(rs.getString(1));
                            beRemove.append(" ' ,");
                        }
                        //有需要删除的记录
                        if (beRemove.length() > 3) {
                            beRemove.setLength(beRemove.length() - 3);
                            beRemove.append(")");
                            //删除所有“超过指定时间为重新请求的记录”

                            dd.modify("delete from online_inf  where session_id in" + beRemove.toString());

                        }
                        dd.closeConn();
                    }
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    } catch (Exception e2) {
                    e2.printStackTrace();
                }
                    }
        }).start();
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.
      */
    }

    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }

    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}
